package com.fast.common.pager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.ibatis.session.RowBounds;

/**
 * @author Windy 分页对象
 */
public class Pageable extends RowBounds implements Serializable {

	private static final long serialVersionUID = -6414350656252331011L;

	public final static int DEFAULT_PAGE_NO = 1;
	public final static int DEFAULT_PAGE_SIZE = 10;
	/**
	 * 页号
	 */
	protected int pageNo = DEFAULT_PAGE_NO;
	/**
	 * 分页大小
	 */
	protected int pageSize = DEFAULT_PAGE_SIZE;
	/**
	 * 分页排序信息
	 */
	protected List<Order> orders = new ArrayList<Order>();
	/**
	 * 总记录数
	 */
	private int total;

	public Pageable() {
	}

	public Pageable(int pageNo, int pageSize) {
		this(pageNo, pageSize, new ArrayList<Order>());
	}

	public Pageable(List<Order> orders) {
		this(0, DEFAULT_PAGE_SIZE, orders);
	}

	public Pageable(Order... order) {
		this(0, DEFAULT_PAGE_SIZE, order);
	}

	public Pageable(int pageNo, int pageSize, Order... order) {
		this(pageNo, pageSize, Arrays.asList(order));
	}

	/**
	 * 总页数
	 * 
	 * @return
	 */
	public Integer getTotalPage() {
		if (pageSize <= 0) {
			pageSize = 10;
		}
		int totalPage = (total - 1) / pageSize + 1;
		return totalPage;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public Integer getNext() {
		Integer nextPage = pageNo + 1;
		if (nextPage > getTotalPage()) {
			return getTotalPage();
		}
		return nextPage;
	}

	/**
	 * 上一页
	 * 
	 * @return
	 */
	public Integer getPrev() {
		int prePage = pageNo - 1;
		if (prePage <= 0) {
			prePage = 1;
		}
		return prePage;
	}

	public Pageable(int pageNo, int limit, List<Order> orders) {
		this.pageNo = pageNo;
		this.pageSize = limit;
		this.orders = orders;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}

	protected int getEnd() {
		return pageSize;
	}

	protected int getStart() {
		if (pageNo >= 1) {
			return (pageNo - 1) * pageSize;
		}
		return 0;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	/**
	 * 
	 *@return 获取{@link #pageNo}
	 */
	public int getPageNo() {
		return pageNo;
	}

	/** 
	 *
	 *@param pageNo 设置{@link #pageNo} 
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	/**
	 * 
	 *@return 获取{@link #pageSize}
	 */
	public int getPageSize() {
		return pageSize;
	}

	/** 
	 *
	 *@param pageSize 设置{@link #pageSize} 
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
 
}